﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using RooHui.Infrastructure.Entities.EFContext;

namespace RooHui.Infrastructure.Entities.Repository
{
    [Table("P_ProcessDef")]
    public class P_ProcessDef : IEntity<P_ProcessDef>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string ProcessDefName { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(100)]
        public string XmlUrl { get; set; }
        public int ProcessType { get; set; }
        public int Status { get; set; }
        public DateTime CreateTime { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(20)]
        public string CreateUser { get; set; }

        public override P_ProcessDef Find(string key)
        {
            int Id = Int32.Parse(key);
            return Find(c => c.ID == Id);
        }

        public override P_ProcessDef Find(System.Linq.Expressions.Expression<Func<P_ProcessDef, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.ProcessDef.FirstOrDefault(where);
                if (item != null)
                {
                    this.CreateTime = item.CreateTime;
                    this.CreateUser = item.CreateUser;
                    this.ID = item.ID;
                    this.ProcessDefName = item.ProcessDefName;
                    this.ProcessType = item.ProcessType;
                    this.XmlUrl = item.XmlUrl;
                    this.Status = item.Status;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<P_ProcessDef, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.ProcessDef.Any(where);
            }
        }
    }
}